package com.david.mydemo.util;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

@Slf4j
public class MyUtil {

    private static final String PHO_HEX_KEY = "7E9BCF1DA17E1088CAB75043CCF439E1";
    private static final String PWD_HEX_KEY = "B7A88977504D9CB54176A1452CB5F43E";

    public static String uuid() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    public static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    /**
     * 获取加密手机号码信息
     * <p>13288889999转换132****9999</p>
     *
     * @param phone
     * @return
     */
    public static JSONObject encryptPhone(String phone) {
        JSONObject jsonObject = new JSONObject();
        // 11位号码
        if (StringUtils.length(phone) != 11) {
            return jsonObject;
        }
        String encryptPhone = phone.substring(0, 3) + "****" + phone.substring(7, 11);
        String encryptMidPhone = JasyptUtil.encryptPBE(phone.substring(3, 7), PHO_HEX_KEY + phone.substring(7, 11));
        jsonObject.put("mid", SmsUtil.generateValidateCode4String(4) + phone.substring(3, 7) + SmsUtil.generateValidateCode4String(4));
        jsonObject.put("encryptPhone", encryptPhone);
        jsonObject.put("encryptMidPhone", encryptMidPhone);
        return jsonObject;
    }

    /**
     * 获取解密手机号码信息
     * <p>132****9999转换13288889999</p>
     *
     * @param encryptPhone
     * @param encryptMidPhone
     * @return
     */
    public static String decryptPhone(String encryptPhone, String encryptMidPhone) {
        if (StringUtils.isEmpty(encryptPhone) || StringUtils.isEmpty(encryptMidPhone)) {
            return null;
        }
        String decryptMidPhone = JasyptUtil.decryptPBE(encryptMidPhone, PHO_HEX_KEY + encryptPhone.substring(7, 11));
        String phone = encryptPhone.substring(0, 3) + decryptMidPhone + encryptPhone.substring(7, 11);
        return phone;
    }

    /**
     * 获取加密密码信息
     *
     * @param pwd
     * @return
     */
    public static String encryptPassword(String pwd) {
        if (StringUtils.isEmpty(pwd)) {
            return null;
        }
        String encryptPwd = MD5Utils.getSaltMD5(pwd);
        return encryptPwd;
    }

    /**
     * 对比密码
     *
     * @param pwd
     * @return
     */
    public static boolean comparePassword(String pwd, String pwdDB) {
        if (StringUtils.isEmpty(pwdDB) || StringUtils.isEmpty(pwd)) {
            return false;
        }
        boolean checkSaltMD5 = MD5Utils.checkSaltMD5(pwd, pwdDB);
        return checkSaltMD5;
    }


    public static void main(String[] args) {
        String phone = "13288889999";
        System.out.println(phone.substring(0, 3) + "****" + phone.substring(7, 11));
        System.out.println(phone.substring(3, 7));

        System.out.println(encryptPhone("13633332222"));
        // {"encryptPhone":"138****6666","encryptMidPhone":"crpQuMegRuEQr299BkvD3Q=="}
        System.out.println(decryptPhone("138****6666", "crpQuMegRuEQr299BkvD3Q=="));
        System.out.println(encryptPassword("admin123"));

        System.out.println(UUID.randomUUID().toString());
        System.out.println(encryptPhone("13633332222"));
        System.out.println(decryptPhone("132****8856", "S0G3Ua6VsjhQngpAnHRRMw=="));
        System.out.println(decryptPhone("132****8889", "irJOiTvFPExiwBGN4YQOrA=="));
        System.out.println(decryptPhone("123****8111", "oF5ieTycJnJ/opUC3BSFvQ=="));
    }

}
